package com.allwees.bs.module.pms.req;

import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.bs.core.modelbase.exception.AuthException;
import com.allwees.bs.core.modelbase.exception.AuthExceptionEnum;
import com.allwees.bs.core.modelbase.req.BoCrudReq;
import com.allwees.bs.core.modelbase.validate.ValidateItem;
import com.allwees.bs.module.pms.entity.Product;
import com.allwees.core.common.util.http.RequestContext;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.ResourceBundle;

@Data
@EqualsAndHashCode(callSuper = true)
public class ProductInfoReq extends BoCrudReq<Product> {
	private static final long serialVersionUID = -8150010368408955287L;
	private boolean autoApply;

	private List<SkuReq> skus;

	private List<String> dels;

	@Override
	public ValidateItem[] getValidateItems() {
		ResourceBundle rb = RequestContext.getContext().getResourceBundle();
		return ArrayUtils.addAll(
				ArrayUtils.addAll(
					new ValidateItem[] {
							new ValidateItem(bo == null, "bo cannot be a null"),
							new ValidateItem(StringUtils.isBlank(bo.getCategoryNo()), rb.getString("validator.product_categoryNo_not_null")),
							new ValidateItem(StringUtils.isBlank(bo.getCustomNo()), rb.getString("validator.product_customNo_not_null")),
							new ValidateItem(bo.getCustomNo() != null && bo.getCustomNo().length() > 32, rb.getString("validator.product_customNo_too_long")),
							new ValidateItem(StringUtils.isBlank(bo.getName()), rb.getString("validator.product_name_not_null")),
							new ValidateItem(StringUtils.isBlank(bo.getDescription()), rb.getString("validator.product_description_not_null")),
							new ValidateItem(StringUtils.isBlank(bo.getKeywords()), rb.getString("validator.product_keywords_not_null")),
							new ValidateItem(StringUtils.isBlank(bo.getMainPhoto()), rb.getString("validator.product_main_photo_not_null"))
						},
					getValidateItem4BranchType()
				),
				getSkuValidateItems()
			);
	}

	private ValidateItem[] getValidateItem4BranchType() {
		ResourceBundle rb = RequestContext.getContext().getResourceBundle();
		switch(getBranchType()) {
			case RETAILER:
				return new ValidateItem[] {
						new ValidateItem(StringUtils.isBlank(bo.getStoreUuid()), rb.getString("validator.product_store_uuid_not_null")),
						new ValidateItem(StringUtils.isBlank(bo.getCurrency()), rb.getString("validator.product_currency_not_null")),
						new ValidateItem(StringUtils.isBlank(bo.getShippingArrivalDesc()), rb.getString("validator.product_shipping_arrival_desc_not_null"))
					};
			case SUPPLIER:
			case STORE:
				return new ValidateItem[0];
			default:
				throw new AuthException(AuthExceptionEnum.WRONG_BRANCH_TYPE);
		}
	}

	private ValidateItem[] getSkuValidateItems() {
		ValidateItem[] validateItems = new ValidateItem[0];
		EBranchType branchType = getBranchType();
		for(SkuReq skuReq : skus) {
			validateItems = ArrayUtils.addAll(validateItems, skuReq.getValidateItems(branchType));
		}
		return validateItems;
	}

	@Override
	protected void completion() {
		super.completion();

		_updateStatInfo();
		_updateMerchantUuid();
	}

	private void _updateStatInfo() {
		bo.setTotalSales(bo.getTotalSales() == null ? 0 : bo.getTotalSales());
		bo.setTotalRefunds(bo.getTotalRefunds() == null ? 0 : bo.getTotalRefunds());
		bo.setTotalCollections(bo.getTotalCollections() == null ? 0 : bo.getTotalCollections());
		bo.setTotalReviews(bo.getTotalReviews() == null ? 0 : bo.getTotalReviews());
		bo.setTotalViews(bo.getTotalViews() == null ? 0 : bo.getTotalViews());
		bo.setRating(bo.getRating() == null ? 0.0f : bo.getRating());
		bo.setRefundRate(bo.getRefundRate() == null ? 0.0f : bo.getRefundRate());
	}

	/**
	 * 根据当前用户不同Merchant类型更新对应uuid值
	 *
	 */
	private void _updateMerchantUuid() {
		switch(getBranchType()) {
			case SUPPLIER:
				getBo().setSupplierUuid(getCurrentSupplierUuid());
				break;
			case RETAILER:
				getBo().setRetailerUuid(getCurrentRetailerUuid());
				break;
			case STORE:
				getBo().setStoreUuid(getCurrentStoreUuid());
				break;
			default:
				throw new AuthException(AuthExceptionEnum.WRONG_BRANCH_TYPE);
		}
	}
}
